package com.cf.compent.rank.impl;

import com.cf.core.player.UserInfo;
import com.cf.compent.rank.UserKeyFormatter;
import com.cf.core.player.UserInfoService;
import com.cf.core.player.impl.DefaultUserInfo;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * game-server-DefaultUserKeyFormater
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/5/10 下午 05:02
 */
public class DefaultUserKeyFormatter implements UserKeyFormatter {

    private String splitKey = "";

    @Autowired
    private UserInfoService userInfoService;

    @Override
    public String encodeUserKey(String userId) {
        UserInfo userInfo = userInfoService.getUserInfo(userId);
        return userInfo.getUserId() + splitKey + userInfo.getName() + splitKey + userInfo.getHead();
    }

    @Override
    public String encodeUserKey(String userId, String userName, String userHead) {
        return userId + splitKey + userName + splitKey + userHead;
    }


    @Override
    public UserInfo decodeUserInfo(String key) {
        String[] split = key.split(splitKey);
        if (split != null && split.length == 3) {
            return new DefaultUserInfo(Integer.parseInt(split[0]), "", split[1], split[2]);
        }
        return null;
    }
}
